﻿/////////////////////////////////////////////////////////////////
//File: BlogInfo.cs
//Description: 
//Copyright© 2011 Moliu
//History:
//  2011-3-21 1:08:21    CodeSmith5.0.1    Original Version

/////////////////////////////////////////////////////////////////

using MAF.Common;
using My.Blog.Web.Controllers;
using System;
using System.Reflection;

namespace My.Blog.Web
{
    public class WebUI
    {
        public static BaseUI<BlogHandler, MyBlog> BlogUI { get; private set; }
        public static SecurityUI SecurityUI { get; private set; }
        public static BlogSystemUI BlogSystemUI { get; private set; }

        static WebUI()
        {
            BlogUI = new BaseUI<BlogHandler, MyBlog>(new BlogHandler());
            SecurityUI = new SecurityUI();
            BlogSystemUI = new BlogSystemUI();
        }

        /// <summary>
        /// 此方法重UIHelper里拷贝过来的，主要是为了不用传递程序集的信息
        /// </summary>
        /// <param name="uiName"></param>
        /// <param name="uiMethod"></param>
        /// <returns></returns>
        public static string ExecuteNonQuery(string uiName, string uiMethod)
        {
            var arry = uiName.Split(". ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (2 > arry.Length)
                throw new ArgumentException("ui的名字，必须是“命名空间.类名.静态属性名”，例如My.Blog.WebUI.ArticleUI");

            var len = arry.Length - 1;
            var arry1 = new string[len];
            Array.Copy(arry, arry1, len);

            Type t = Type.GetType(string.Join(".", arry1), true, true);
            // 这里有点疑惑，为什么加上BindingFlags.Static就会失败
            object obj = t.InvokeMember(arry[len], BindingFlags.GetProperty | BindingFlags.GetField, null, null, null);
            // 这里有点疑惑，这里为什么可以用obj来调用静态方法
            return (string)(obj.GetType().InvokeMember(uiMethod, BindingFlags.InvokeMethod, null, obj, null));
        }
    }
}
